/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.InqueryDAOInterface;
import com.wii.elpro.dao.hb.CabangDAOImpl;
import com.wii.elpro.dao.hb.InqueryDAOImpl;
import com.wii.elpro.dao.hb.ProjectDAOImpl;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.InquiryGetByPCForm;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author JollkY
 */
public class InquiryGetInquiriesByPCAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        InquiryGetByPCForm f = (InquiryGetByPCForm) form;
        InqueryDAOInterface dao = new InqueryDAOImpl();        
        ActionMessages messages = new ActionMessages();
        try {
            List<Inquery> inquiries = dao.getInqueries(new ProjectDAOImpl().getProject(f.getProject()), new CabangDAOImpl().getCabang(f.getCabang()));
            request.setAttribute("inquiries", inquiries);
            messages.add("customMessage", new ActionMessage("customMessage", "Inquiry of project "+new ProjectDAOImpl().getProject(f.getProject()).getNama()+" in "+new CabangDAOImpl().getCabang(f.getCabang()).getKota()));
            forward = "success";
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve data. Please contact my developer."));
            ex.printStackTrace();
            forward = "fail";
        }
        saveMessages(sess, messages);
        return mapping.findForward(forward);
    }
}
